﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;

namespace HRM
{
    public partial class frmShiftList : DockContent
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int ShiftID;
        ShiftClient wcfShift;
        RoleRightInfo listRole;
        DepartmentClient wcfDept;
        int _DeptID = 0;
        public frmShiftList()
        {
            InitializeComponent();
            this.KeyPreview = true;
            LoadLanguage();
            BuildVoucherGrid();
            wcfDept = Service.Department();
            wcfShift = Service.Shift();
            listRole = UIParamater.Quyen_CaLamViec;

            UISetting.LoadTreeView3(ref tvBranch);
            if (!listRole.Update)
            {
                this.btnEdit.Image = global::HRM.Properties.Resources.app;
                btnEdit.Text = "xem";
            }
            LoadData();
            
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = this.TabText = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_ContextMenuStrip(ref mnuPopup);
        }
        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvShiftList, "Shift_ID", "", "", 1, false, true);
            UISetting.AddColumn(ref dgvShiftList, "Shift_DepartmentID", "", "", 1, false, true);
            UISetting.AddColumn(ref dgvShiftList, "RowNumber", "", "C", 50, true, true);
            UISetting.AddColumn(ref dgvShiftList, "Department_Name", "", "", 120, true, true);
            UISetting.AddColumn(ref dgvShiftList, "Shift_Name", "", "", 120, true, true);
            UISetting.AddColumn(ref dgvShiftList, "Shift_TimeIn", "", "", 120, true, true);
            UISetting.AddColumn(ref dgvShiftList, "Shift_TimeOut", "", "", 120, true, true);
            UISetting.AddColumn(ref dgvShiftList, "Shift_TimeInB", "", "", 120, true, true);
            UISetting.AddColumn(ref dgvShiftList, "Shift_TimeOutB", "", "", 120, true, true);
            UISetting.AddColumn(ref dgvShiftList, "Shift_Description", "", "", 200, true, true);
            
        }

        private void LoadData()
        {
            DataSet _ds = wcfShift.Shift_GetList(out itemCount, currPage, pageSize, _DeptID);
            PageIndex();
            dgvShiftList.Rows.Clear();
            for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
            {
                dgvShiftList.Rows.Insert(i, 1);
                dgvShiftList.Rows[i].Cells["Shift_ID"].Value = _ds.Tables[0].Rows[i]["Shift_ID"].ToString();
                dgvShiftList.Rows[i].Cells["Shift_DepartmentID"].Value = _ds.Tables[0].Rows[i]["Shift_DepartmentID"].ToString();
                dgvShiftList.Rows[i].Cells["Shift_Name"].Value = _ds.Tables[0].Rows[i]["Shift_Name"].ToString();
                dgvShiftList.Rows[i].Cells["RowNumber"].Value = _ds.Tables[0].Rows[i]["RowNumber"].ToString();
                dgvShiftList.Rows[i].Cells["Department_Name"].Value = _ds.Tables[0].Rows[i]["Department_Name"].ToString();
                dgvShiftList.Rows[i].Cells["Shift_TimeIn"].Value = _ds.Tables[0].Rows[i]["Shift_TimeIn"].ToString();
                dgvShiftList.Rows[i].Cells["Shift_TimeOut"].Value = _ds.Tables[0].Rows[i]["Shift_TimeOut"].ToString();
                dgvShiftList.Rows[i].Cells["Shift_TimeInB"].Value = _ds.Tables[0].Rows[i]["Shift_TimeInB"].ToString();
                dgvShiftList.Rows[i].Cells["Shift_TimeOutB"].Value = _ds.Tables[0].Rows[i]["Shift_TimeOutB"].ToString();
                dgvShiftList.Rows[i].Cells["Shift_Description"].Value = _ds.Tables[0].Rows[i]["Shift_Description"].ToString();
            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            Form f = new frmShiftInfo(0, listRole);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvShiftList.SelectedRows.Count > 0)
            {
                if (!listRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                ShiftID = Convert.ToInt32(dgvShiftList.CurrentRow.Cells["Shift_ID"].Value);
                if (wcfShift.Shift_Using(ShiftID) != 0)
                {
                    MessageBox.Show(UpdateUI.GetString("dasudung"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (wcfShift.Shift_Delete(ShiftID))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvShiftList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {

                    ShiftID = Convert.ToInt32(dgvShiftList.CurrentRow.Cells["Shift_ID"].Value);
                    Form f = new frmShiftInfo(ShiftID, listRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage < totalPage - 1)
                currPage++;

            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage > 0)
                currPage--;

            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage > 0)
                currPage = totalPage - 1;

            LoadData();
        }

        private void dgvShiftList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
                btnEdit_Click(null, null);
        }

        private void frmShiftList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
                case Keys.Delete:
                    btnDel_Click(null, null);
                    break;
            }
        }

        private void frmShiftList_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnNewPopUp_Click(object sender, EventArgs e)
        {
            btnNew_Click(null, null);
        }

        private void btnEditPopUp_Click(object sender, EventArgs e)
        {
            btnEdit_Click(null, null);
        }

        private void btnDelPopUp_Click(object sender, EventArgs e)
        {
            btnDel_Click(null, null);
        }

        private void dgvShiftList_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (dgvShiftList.SelectedRows.Count > 0)
                {
                    mnuPopup.Enabled = true;
                    btnNewPopUp.Enabled = true;
                    btnEditPopUp.Enabled = true;
                    btnDelPopUp.Enabled = true;
                }
            }
        }

        private void frmShiftList_Activated(object sender, EventArgs e)
        {
            LoadLanguage();
            if (dgvShiftList.Columns.Count > 0)
            {
                dgvShiftList.Columns["Rownumber"].HeaderText = UpdateUI.GetString("stt");
                dgvShiftList.Columns["Shift_Name"].HeaderText = UpdateUI.GetString("ten");
                dgvShiftList.Columns["Department_Name"].HeaderText = UpdateUI.GetString("phongban");
                dgvShiftList.Columns["Shift_TimeIn"].HeaderText = UpdateUI.GetString("thoigianvao");
                dgvShiftList.Columns["Shift_TimeOut"].HeaderText = UpdateUI.GetString("thoigianra");
                dgvShiftList.Columns["Shift_TimeInB"].HeaderText = UpdateUI.GetString("thoigianvaob");
                dgvShiftList.Columns["Shift_TimeOutB"].HeaderText = UpdateUI.GetString("thoigianrab");
                dgvShiftList.Columns["Shift_Description"].HeaderText = UpdateUI.GetString("ghichu");
            }
        }

        private void tvBranch_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 0)
            {
                _DeptID = 0;
                LoadData();
            }
            else if (e.Node.Level == 1)
            {
                _DeptID = 0;
                currPage = 0;
                LoadData();
            }
            else if (e.Node.Level >= 2)
            {
                _DeptID = Convert.ToInt32(e.Node.Tag);
                currPage = 0;
                LoadData();
            }

        }

        private void frmShiftList_FormClosing(object sender, FormClosingEventArgs e)
        {
            wcfDept.Close();
            wcfShift.Close();
            
        }
    }
}
